/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.nus.iss.t5.jass.dao;

import edu.nus.iss.t5.jass.entity.Address;
import edu.nus.iss.t5.jass.entity.JobSeeker;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;

/**
 * This JobSeekerDAO class will load seeker.properties file to retrive Job Seeker information.
 * @author Fenglei
 */
public class JobSeekerDAO {

    private static String seekerPropFile = "data\\seeker.properties";
    private static String key = "id.";
    private final String name = "name.";
    private final String address_city = "address.city.";
    private final String address_country = "address.country.";
    private final String address_street = "address.street.";
    private final String address_postcode = "address.postcode.";
    private final String email = "email.";
    private final String mobile = "mobile.";
    private final String tel = "telephone.";

    private Properties props;
    private Hashtable<String, JobSeeker> jobSeekerList;
  

    /**
     * This constructor will load seeker.properties file.
     * @throws IOException  if the system cannot find seeker.properties, the exception will be thrown.
     */
    public JobSeekerDAO() throws IOException {
        props = new Properties();

        File f = new File(seekerPropFile);
        if (f.exists()) {

            FileInputStream in = new FileInputStream(f);
            props.load(in);
            in.close();
            jobSeekerList = new Hashtable<String, JobSeeker>();

        } else {
            throw new IOException("The system cannot find file: " + this.seekerPropFile);
        }
    }

    private void getAll() {
        if (jobSeekerList == null) {
            jobSeekerList = new Hashtable<String, JobSeeker>();
        } else {
            jobSeekerList.clear();
        }

        Set<Object> keys = props.keySet();
        Iterator itr = keys.iterator();
        String tempKey="";
        String id = "";
        while (itr.hasNext()) {
            tempKey = (String) itr.next();
            if (tempKey.startsWith(key)) {
                JobSeeker seeker = new JobSeeker();
                seeker.setId(props.getProperty(tempKey));
                id = seeker.getId();
                seeker.setEmail(props.getProperty(email + id));
                seeker.setMobilephone(props.getProperty(mobile+id));
                seeker.setName(props.getProperty(name+id));
                seeker.setTelephone(props.getProperty(tel+id));
                Address address=new Address();
                address.setCity(props.getProperty(address_city+id));
                address.setCountry(props.getProperty(address_country+id));
                address.setStreet(props.getProperty(address_street+id));
                address.setPostcode(props.getProperty(address_postcode+id));
                seeker.setAddress(address);

                jobSeekerList.put(id, seeker);
            }

        }
    }

/**
 *  Retrieve Job seeker entity object by given seeker Id.
 * @param seekerId the unique identity for Job Seeker
 * @return The Job Seeker entity object.
 * Return null if the system cannot find Job Seeker object by given ID.
 */
    public JobSeeker getJobSeeker(String seekerId) {
        if (jobSeekerList == null || jobSeekerList.isEmpty()) {
            this.getAll();
        }
        JobSeeker seeker = jobSeekerList.get(seekerId);

        return seeker;
    }

}
